﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisTrainingInfo : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private SsMng creatby = new SsMng();
        private Main main = new Main();
        private HisTrainingBLL BLL = new HisTrainingBLL();
        private ITrainingBLL trBLL = new TrainingBLL();
        private ITrainingHistoryBLL CreateTrainingHis = new TrainingHistoryBLL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);

                if (Request.QueryString["sta"] != null)
                    this.lblSta.Text = Request.QueryString["sta"].ToString();

                lblEmpSeq.Text = Session["emp_seq"].ToString();
                Panel5.Visible = true;
                pnlGridHisTrain.Visible = false;
                LoadDataGrid();
                LoadHisTraining();
                
            }
        }
        public void LoadDataGrid()
        {
                DataSet ds = trBLL.LoadTrainingHistories(Convert.ToInt32(lblEmpSeq.Text));
                DataTable dt = new DataTable();
                dt = ds.Tables[0].Copy();

                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                //int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                //BLL.Delete(seq, ssMng.EmpSeqI);
            }
            LoadDataGrid();
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("cmdAddNew") as LinkButton;

                if (this.lblSta.Text == "v")
                {
                    button.Enabled = false;
                    button.Visible = true;
                    button.Text = "";
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (this.lblSta.Text == "v")
            {
                RadGrid1.Columns[0].Visible = false;
                RadGrid1.Columns[1].Visible = false;
            }
        }
    
      // ================================================= ประวัติการอบรมเก่า =============================================================

        private void LoadHisTraining()
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;

            RadGrid2.DataSource = CreateTrainingHis.Search(null, strQueryString, null);
            RadGrid2.DataBind();
      }

        protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;

            RadGrid2.DataSource = CreateTrainingHis.Search(null, strQueryString, null);
        }

        protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("cmdAddNew") as LinkButton;

                if (this.lblSta.Text == "v")
                {
                    button.Enabled = false;
                    button.Visible = true;
                    button.Text = "";
                }
            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
                if (e.CommandName == "Delete")
                {
                    int intTrainHisId = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TrainHisId"]);
                    string strQueryString = main.WebString((e.Item as GridDataItem)["emp_seq"].Text);

                    bool CheckValueDelete = CreateTrainingHis.CheckValueDelete(intTrainHisId);
                    if (CheckValueDelete == true)
                    {
                        CreateTrainingHis.Delete(creatby.EmpSeqI, intTrainHisId, strQueryString);
                        main.MessageAlert(Page, "ลบข้อมูลรียบร้อยแล้ว");
                    }
                }
                LoadHisTraining();
            }
     
        protected void rdbTraining_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdbTraining.SelectedValue == "Training")
            {
                LoadDataGrid();
                Panel5.Visible = true;
                pnlGridHisTrain.Visible = false;
            }
            else if (rdbTraining.SelectedValue == "HisTraining")
            {
                LoadHisTraining();
                Panel5.Visible = false;
                pnlGridHisTrain.Visible = true;
            }
            //Training = rdbTraining.SelectedValue;
            //LoadDataGrid();
        }
    }
 }
